﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Ajax_SubmitWin : System.Web.UI.Page
{
    static decimal oneMoney = 0;
    static decimal twoMoney = 0;
    static decimal threeMoney = 0;
    static decimal sumWinMoney = 0;
    static Model.LotteryAnnouncement la = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpContext c = HttpContext.Current;
        string rtn = "fail";
        int lotteryType = ReadRequest.RequestInt("type");
        string lotteryQs = ReadRequest.RequestString("qs");
        string sql = "select * from LotteryAnnouncement where LotteryType=" + lotteryType + " and LotteryQs=" + lotteryQs;
        Model.LotteryAnnouncement model = BLL.LotteryAnnouncement.GetModelBySql(sql);
        if (model != null)
        {
            bool issuccess=TraverseTheWinning(lotteryQs, model.LotteryNo, lotteryType);
            if (issuccess)
            {
                rtn = "success";
            }
        }


        Response.Write(rtn);
        Response.End();
    }

    /// <summary>
    /// 遍历中奖情况
    /// </summary>
    /// <param name="lotteryQs">彩票期数</param>
    /// <param name="lotteryNumber">开奖号码</param>
    /// <param name="lotteryType">彩票类型</param>
    /// <returns>成功 失败</returns>
    public static bool TraverseTheWinning(string lotteryQs, string lotteryNumber, int lotteryType)
    {
        List<Model.LotteryBuyDetail> userBuyInfo = new List<Model.LotteryBuyDetail>();
        int totalcount = 0;
        string sql = "Flag=0 and LotteryType=" + lotteryType + " and  LotteryQs=" + lotteryQs;
        userBuyInfo = BLL.LotteryBuyDetail.GetPageListBysql(0, int.MaxValue, sql, out totalcount);
        if (userBuyInfo != null)
        {
            //双色球
            if (lotteryType == 1)
            {
                for (int i = 0; i < userBuyInfo.Count; i++)
                {
                    int[] level = calculateRed(lotteryNumber, userBuyInfo[i].LotteryNo);
                    //更新数据库
                    updateUserAccoutBySSQ(level, userBuyInfo[i].User_id, userBuyInfo[i].id);
                    addWinInfo(level, lotteryType, lotteryQs, userBuyInfo[i].id);
                }
            }//大乐透
            else if (lotteryType == 2)
            {
                for (int i = 0; i < userBuyInfo.Count; i++)
                {
                    int[] level = calculateDLT(lotteryNumber, userBuyInfo[i].LotteryNo);
                    //更新数据库
                    updateUserAccoutByDLT(level, userBuyInfo[i].User_id, userBuyInfo[i].id);
                    addWinInfo(level, lotteryType, lotteryQs, userBuyInfo[i].id);
                }
            }//排列三
            else if (lotteryType == 3)
            {
                for (int i = 0; i < userBuyInfo.Count; i++)
                {
                    string[] userLotteryNo = userBuyInfo[i].LotteryNo.Split(',');
                    string levestr = "";
                    for (int n = 0; n < userLotteryNo.Length; n++) {
                        int level = calculatePLS(lotteryNumber, userLotteryNo[n]);
                        levestr += ","+level.ToString();
                    }
                    levestr = levestr.TrimStart(',');
                    //更新数据库
                    updateUserAccoutByPLS(levestr, userBuyInfo[i].User_id, userBuyInfo[i].id);
                }
            }//排列五
            else if (lotteryType == 4)
            {
                for (int i = 0; i < userBuyInfo.Count; i++)
                {
                    calculatePLW(lotteryNumber, userBuyInfo[i].LotteryNo, userBuyInfo[i].User_id, userBuyInfo[i].id);
                }
            }//福彩3D
            else if (lotteryType == 5)
            {
                for (int i = 0; i < userBuyInfo.Count; i++)
                {   //更新数据库(string winSSQStr, string SSQStr, int userID, int LotteryBuyDetailID, int fcType, int fcTypeNext)
                    calculateFC(lotteryNumber, userBuyInfo[i].LotteryNo, userBuyInfo[i].User_id, userBuyInfo[i].id, userBuyInfo[i].fcType, userBuyInfo[i].fcTypeNext); 
                }
            }//七星彩
            else if (lotteryType == 6)
            {
                for (int i = 0; i < userBuyInfo.Count; i++)
                {
                    string[] userLotteryNo = userBuyInfo[i].LotteryNo.Split(',');
                    string levestr = "";
                    for (int n = 0; n < userLotteryNo.Length; n++)
                    {
                        int level = calculateQXC(lotteryNumber, userLotteryNo[n]); 
                        levestr += "," + level.ToString();
                    }
                    levestr = levestr.TrimStart(',');
                    //更新数据库
                    updateUserAccoutByQXC(levestr, userBuyInfo[i].User_id, userBuyInfo[i].id);
                }
            }//七乐彩
            else if (lotteryType == 7)
            {
                for (int i = 0; i < userBuyInfo.Count; i++)
                {
                    string[] userLotteryNo = userBuyInfo[i].LotteryNo.Split(',');
                    string levestr = "";
                    for (int n = 0; n < userLotteryNo.Length; n++)
                    { 
                        int level = calculateQLC(lotteryNumber, userLotteryNo[n]);
                        levestr += "," + level.ToString();
                    }
                    levestr = levestr.TrimStart(',');
                    //更新数据库
                    updateUserAccoutByQLC(levestr, userBuyInfo[i].User_id, userBuyInfo[i].id);
                }
            }
            return true;
        }
        else
        {
            return false;
        }
    }
    /// <summary>
    /// 双色球中奖
    /// </summary>
    /// <param name="winSSQStr">开奖号码</param>
    /// <param name="SSQStr">用户购买的号码</param>
    /// <returns>等级</returns>
    private static int[] calculateRed(string winSSQStr, string SSQStr)
    {
        int dmRedCount = 0;
        int tmRedCount = 0;
        int dtBlueCount = 0;
        int[] level = new int[6];
        //中奖号
        string[] winSSQList = winSSQStr.Split('+');
        string[] winRed = winSSQList[0].Split(',');
        string[] winBlue = winSSQList[1].Split(',');
        //用户号码
        //SSQStr = SSQStr.Replace("$","*");
        string[] SSQ = SSQStr.Split(',');
        for (int n = 0; n < SSQ.Length; n++)
        {
            if (SSQ[n].Contains("$"))
            {
                string[] SSQList = SSQ[n].Split('+');
                string[] userRed = SSQList[0].Split('$');
                string[] userDm = userRed[0].Split('*');
                string[] userTm = userRed[1].Split('*');
                string[] UserBlue = SSQList[1].Split('*');
                for (int i = 0; i < winRed.Length; i++)
                {
                    for (int j = 0; j < userDm.Length; j++)
                    {
                        if (int.Parse(winRed[i]) == int.Parse(userDm[j]))
                        {
                            dmRedCount++;
                            break;
                        }
                    }
                }
                for (int i = 0; i < winRed.Length; i++)
                {
                    for (int j = 0; j < userTm.Length; j++)
                    {
                        if (int.Parse(winRed[i]) == int.Parse(userTm[j]))
                        {
                            tmRedCount++;
                            break;
                        }
                    }
                }
                //比对蓝球
                for (int i = 0; i < winBlue.Length; i++)
                {
                    for (int j = 0; j < UserBlue.Length; j++)
                    {
                        if (int.Parse(winBlue[i]) == int.Parse(UserBlue[j]))
                        {
                            dtBlueCount++;
                            break;
                        }
                    }
                }
                int b = userTm.Length;
                int e = userDm.Length;
                int r = UserBlue.Length;
                int g = dmRedCount;
                int s = tmRedCount;
                int o = dtBlueCount;
                var u = s <= 6 - e ? s : 6 - e;
                int[] a = new int[10];
                for (var f = 0; f <= u; f++)
                {
                    a[f] = g + f;
                }
                int c = 0;
                if (o == 1)
                {
                    for (var f = 0; f <= u; f++)
                    {
                        c = Rank(a[f], 0);
                        if (c != -1)
                        {
                            level[c] += Ccombo(b - s, 6 - e - f) * (r - 1) * Ccombo(s, f);
                            c = Rank(a[f], 1);
                            level[c] += Ccombo(b - s, 6 - e - f) * Ccombo(s, f);
                        }
                    }
                }
                if (o == 0)
                {
                    for (var f = 0; f <= u; f++)
                    {
                        c = Rank(a[f], 0);
                        if (c != -1)
                        {
                            level[c] += Ccombo(b - s, 6 - e - f) * r * Ccombo(s, f);
                        }
                    }
                }
                //level = l[0].ToString() + "," + l[1] + "," + l[2] + "," + l[3] + "," + l[4]+ "," + l[5];
                //oneZS = l[0]; twoZS = l[1]; threeZS = l[2]; fourZS = l[3]; fiveZS = l[4]; six = l[5];
            }
            else
            {
                string[] SSQList = SSQ[n].Split('+');
                string[] UserRed = SSQList[0].Split('*');
                string[] UserBlue = SSQList[1].Split('*');

                int redCount = 0;
                int blueCount = 0;
                //比对红球
                for (int m = 0; m < winRed.Length; m++)
                {
                    for (int j = 0; j < UserRed.Length; j++)
                    {
                        if (int.Parse(winRed[m]) == int.Parse(UserRed[j]))
                        {
                            redCount++;
                            break;
                        }
                    }
                }
                //比对蓝球
                for (int t = 0; t < winBlue.Length; t++)
                {
                    for (int j = 0; j < UserBlue.Length; j++)
                    {
                        if (int.Parse(winBlue[t]) == int.Parse(UserBlue[j]))
                        {
                            blueCount++;
                            break;
                        }
                    }
                }
                int u = UserRed.Length;
                int r = UserBlue.Length;
                int i = redCount;
                int s = blueCount;
                if (i<3)
                {
                    if (blueCount > 0)
                    {
                        level[5] += combo(u, 6);
                    }
                }
                else if (i == 3)
                {
                    if (blueCount > 0)
                    {
                        level[5] += combo(u - i, 4) * combo(i, 2);
                        level[5] += combo(u - i, 5) * combo(i, 1);
                        level[5] += combo(u - i, 6);
                        level[4] += combo(u - i, 3);
                    }
                }
                else if (i == 4)
                {
                    if (s > 0)
                    {
                        level[5] += combo(u - i, 6);
                        level[5] += combo(u - i, 5) * combo(i, 1);
                        level[5] += combo(u - i, 4) * combo(i, 2);
                        level[4] += combo(u - i, 3) * combo(i, 3);
                        level[4] += combo(u - i, 2) * combo(i, 4) * (r - 1);
                        level[3] += combo(u - i, 2);
                    }
                    else
                    {
                        level[4] += combo(u - i, 2) * r;
                    }
                }
                else if (i == 5)
                {
                    if (s > 0)
                    {
                        level[5] += combo(u - i, 6);
                        level[5] += combo(u - i, 5) * combo(i, 1);
                        level[5] += combo(u - i, 4) * combo(i, 2);
                        level[4] += combo(u - i, 3) * combo(i, 3);
                        level[4] += combo(u - i, 2) * combo(i, 4) * (r - 1);
                        level[3] += combo(u - i, 2) * combo(i, 4);
                        level[3] += combo(u - i, 1) * combo(i, 5) * (r - 1);
                        level[2] += combo(u - i, 1);
                    }
                    else
                    {
                        level[4] += combo(u - i, 2) * combo(i, 4) * r;
                        level[3] += combo(u - i, 1) * combo(i, 5) * r;

                    }
                }
                else if (i == 6)
                {
                    if (s > 0)
                    {
                        level[5] += combo(u - i, 6);
                        level[5] += combo(u - i, 5) * combo(i, 1);
                        level[5] += combo(u - i, 4) * combo(i, 2);
                        level[4] += combo(u - i, 3) * combo(i, 3);
                        level[4] += combo(u - i, 2) * combo(i, 4) * (r - 1);
                        level[3] += combo(u - i, 2) * combo(i, 4);
                        level[3] += combo(u - i, 1) * combo(i, 5) * (r - 1);
                        level[2] += combo(u - i, 1) * combo(i, 5);
                        level[1] += r - 1;
                        level[0]++;
                    }
                    else
                    {
                        level[4] += combo(u - i, 2) * combo(i, 4) * r;
                        level[3] += combo(u - i, 1) * combo(i, 5) * r;
                        level[1] += r;
                    }
                }
                //level = o[0].ToString() + "," + o[1] + "," + o[2] + "," + o[3] + "," + o[4] + "," + o[5];
            }
        }
        return level;
    }
    /// <summary>
    /// 双色球更新中奖信息
    /// </summary>
    /// <param name="level">中奖等级</param>
    /// <param name="userID">用户id</param>
    /// <param name="LotteryBuyDetailID">购买彩票的id</param>
    private static void updateUserAccoutBySSQ(int[] level, int userID, int LotteryBuyDetailID)
    {
        decimal winMoney = 0;
        bool isWin = true;
        int maxLevel = -1;
        string sql = string.Format("SELECT top 1 * FROM LotteryAnnouncement WHERE LotteryType={0} and LotteryQS=(select LotteryQS from LotteryBuyDetail where id={1})", 1, LotteryBuyDetailID);
        la = BLL.LotteryAnnouncement.GetModelBySql(sql);
        if (la != null)
        {
            oneMoney = la.oneMoney;
            twoMoney = la.twoMoney;
        }
        Model.LotteryBuyDetail model = BLL.LotteryBuyDetail.GetModel(LotteryBuyDetailID);
        Model.UsersAccount accout = BLL.UsersAccount.GetModelByuser_id(userID);
        for (int n = 0; n < level.Length; n++)
        {
            if (level[n] != 0)
            {
                maxLevel = n + 1;
            }
            if (n == 0)
            {
                winMoney += oneMoney * level[0];
            }
            else if (n == 1)
            {
                winMoney += twoMoney * level[1];
            }
            else if (n == 2)
            {
                winMoney += 3000 * level[2];
            }
            else if (n == 3)
            {
                winMoney += 200 * level[3];
            }
            else if (n == 4)
            {
                winMoney += 10 * level[4];
            }
            else if (n == 5)
            {
                winMoney += 5 * level[5];
            }
        }
        if (winMoney > 0)
        {
            isWin = true;
        }
        else
        {
            isWin = false;
        }
        sumWinMoney = winMoney;
        model.IsWin = isWin;
        model.WinLevel = maxLevel;
        model.WinMoney = winMoney * model.LotteryMultiple;
        model.Flag = true;
        BLL.LotteryBuyDetail.Update(model);
        accout.total = accout.total + winMoney * model.LotteryMultiple;
        accout.winTotal = accout.winTotal + winMoney * model.LotteryMultiple;
        BLL.UsersAccount.Update(accout);
    }
    /// <summary>
    /// 大乐透中奖
    /// </summary>
    /// <param name="winSSQStr">开奖号码</param>
    /// <param name="SSQStr">用户购买的号码</param>
    /// <returns>等级</returns>
    private static int[] calculateDLT(string winSSQStr, string SSQStr)
    {

        int dmRedCount = 0;
        int tmRedCount = 0;
        int dmBlueCount = 0;
        int tmBlueCount = 0;
        string[] dmUserRed = null;
        string[] tmUserRed = null;
        string[] dmUserBlue = null;
        string[] tmUserBlue = null;
        int[] level = new int[6];
        //中奖号
        string[] winSSQList = winSSQStr.Split('+');
        string[] winRed = winSSQList[0].Split(',');
        string[] winBlue = winSSQList[1].Split(',');
        //用户号码
        //SSQStr = SSQStr.Replace("$","*");
        string[] DLT = SSQStr.Split(',');
        for (int n = 0; n < DLT.Length; n++)
        {
            if (DLT[n].Contains("$"))
            {
                string[] DLTList = DLT[n].Split('+');
                if (DLTList[0].Contains("$"))
                {
                    dmUserRed = DLTList[0].Split('$')[0].Split('*');
                    tmUserRed = DLTList[0].Split('$')[1].Split('*');
                    for (int i = 0; i < winRed.Length; i++)
                    {
                        for (int j = 0; j < dmUserRed.Length; j++)
                        {
                            if (int.Parse(winRed[i]) == int.Parse(dmUserRed[j]))
                            {
                                dmRedCount++;
                                break;
                            }
                        }
                    }
                    for (int i = 0; i < winRed.Length; i++)
                    {
                        for (int j = 0; j < tmUserRed.Length; j++)
                        {
                            if (int.Parse(winRed[i]) == int.Parse(tmUserRed[j]))
                            {
                                tmRedCount++;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    string[] UserRed = DLTList[0].Split('*');
                    for (int i = 0; i < winRed.Length; i++)
                    {
                        for (int j = 0; j < UserRed.Length; j++)
                        {
                            if (int.Parse(winRed[i]) == int.Parse(UserRed[j]))
                            {
                                dmRedCount++;
                                break;
                            }
                        }
                    }
                    tmRedCount = 0;
                }
                if (DLTList[1].Contains("$"))
                {
                    dmUserBlue = DLTList[1].Split('$')[0].Split('*');
                    tmUserBlue = DLTList[1].Split('$')[1].Split('*');
                    for (int i = 0; i < winBlue.Length; i++)
                    {
                        for (int j = 0; j < dmUserBlue.Length; j++)
                        {
                            if (int.Parse(winBlue[i]) == int.Parse(dmUserBlue[j]))
                            {
                                dmBlueCount++;
                                break;
                            }
                        }
                    }
                    for (int i = 0; i < winBlue.Length; i++)
                    {
                        for (int j = 0; j < tmUserBlue.Length; j++)
                        {
                            if (int.Parse(winBlue[i]) == int.Parse(tmUserBlue[j]))
                            {
                                tmBlueCount++;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    string[] userBlue = DLTList[1].Split('*');
                    for (int i = 0; i < winRed.Length; i++)
                    {
                        for (int j = 0; j < userBlue.Length; j++)
                        {
                            if (int.Parse(winBlue[i]) == int.Parse(userBlue[j]))
                            {
                                dmBlueCount++;
                                break;
                            }
                        }
                    }
                    tmBlueCount = 0;
                }
                int r = dmUserRed == null ? 5 : dmUserRed.Length;
                int m = tmUserRed == null ? 0 : tmUserRed.Length;
                int s = dmUserBlue == null ? 2 : dmUserBlue.Length;
                int o = dmUserBlue == null ? 0 : tmUserBlue.Length;
                int u = dmRedCount;
                int a = tmRedCount;
                int f = dmBlueCount;
                int l = tmBlueCount;
                int c = a <= 5 - r ? a : 5 - r;
                int h = l <= 2 - s ? l : 2 - s;
                int[] p = new int[6];
                int[] d = new int[6];
                for (var v = 0; v <= c; v++) p[v] = u + v;
                for (int v = 0; v <= h; v++) d[v] = f + v;
                int g = 0;
                for (int v = 0; v <= c; v++)
                {
                    for (var y = 0; y <= h; y++)
                    {
                        g = Rankx(p[v], d[y]);
                        if (g != -1)
                        {
                            level[g] += Ccombo(m - a, 5 - r - v) * Ccombo(a, v) * Ccombo(o - l, 2 - s - y) * Ccombo(l, y);
                        }
                    }
                }
            }
            else
            {
                string[] SSQList = DLT[n].Split('+');
                string[] UserRed = SSQList[0].Split('*');
                string[] UserBlue = SSQList[1].Split('*');

                int redCount = 0;
                int blueCount = 0;
                //比对红球
                for (int m = 0; m < winRed.Length; m++)
                {
                    for (int j = 0; j < UserRed.Length; j++)
                    {
                        if (int.Parse(winRed[m]) == int.Parse(UserRed[j]))
                        {
                            redCount++;
                            break;
                        }
                    }
                }
                //比对蓝球
                for (int m = 0; m < winBlue.Length; m++)
                {
                    for (int j = 0; j < UserBlue.Length; j++)
                    {
                        if (int.Parse(winBlue[m]) == int.Parse(UserBlue[j]))
                        {
                            blueCount++;
                            break;
                        }
                    }
                }
                int r = UserRed.Length;
                int i = UserBlue.Length;
                int s = redCount;
                int o = blueCount;

                for (var u = 0; u <= s; u++) for (var a = 0; a <= o; a++)
                    {
                        int f = Rankx(u, a);
                        if (f != -1)
                        {
                            level[f] += combo(s, u) * combo(r - s, 5 - u) * combo(i - o, 2 - a) * combo(o, a);
                        }
                    }
            }
        }
        return level;
    }
    /// <summary>
    /// 大乐透更新中奖信息
    /// </summary>
    /// <param name="level">中奖等级</param>
    /// <param name="userID">用户id</param>
    /// <param name="LotteryBuyDetailID">购买彩票的id</param>
    private static void updateUserAccoutByDLT(int[] level, int userID, int LotteryBuyDetailID)
    {
        decimal winMoney = 0;
        bool isWin = true;
        int maxLevel = -1;
        string sql = string.Format("SELECT top 1 * FROM LotteryAnnouncement WHERE LotteryType={0} and LotteryQS=(select LotteryQS from LotteryBuyDetail where id={1})", 2, LotteryBuyDetailID);
        la = BLL.LotteryAnnouncement.GetModelBySql(sql);
        if (la != null)
        {
            oneMoney = la.oneMoney;
            twoMoney = la.twoMoney;
            threeMoney = la.threeMoney;
        }
        Model.LotteryBuyDetail model = BLL.LotteryBuyDetail.GetModel(LotteryBuyDetailID);
        Model.UsersAccount accout = BLL.UsersAccount.GetModelByuser_id(userID);
        for (int n = 0; n < level.Length; n++)
        {
            if (level[n] != 0)
            {
                maxLevel = n + 1;
            }
            if (n == 0)
            {
                winMoney += oneMoney * level[0] * model.LotteryMultiple;
            }
            else if (n == 1)
            {
                winMoney += twoMoney * level[1] * model.LotteryMultiple;
            }
            else if (n == 2)
            {
                winMoney += threeMoney * level[2] * model.LotteryMultiple;
            }
            else if (n == 3)
            {
                winMoney += 200 * model.LotteryMultiple * level[3];
            }
            else if (n == 4)
            {
                winMoney += 10 * model.LotteryMultiple * level[4];
            }
            else if (n == 5)
            {
                winMoney += 5 * model.LotteryMultiple * level[5];
            }
        }
        if (winMoney > 0)
        {
            isWin = true;
        }
        else
        {
            isWin = false;
        }
        sumWinMoney = winMoney;
        model.IsWin = isWin;
        model.WinLevel = maxLevel;
        model.WinMoney = winMoney;
        model.Flag = true;
        BLL.LotteryBuyDetail.Update(model);
        accout.total = accout.total + winMoney;
        accout.winTotal = accout.winTotal + winMoney;
        BLL.UsersAccount.Update(accout);
    }

    /// <summary>
    /// 排列五中奖并更新数据库
    /// </summary>
    /// <param name="winSSQStr">开奖号码</param>
    /// <param name="SSQStr">用户购买的号码</param>
    /// <returns>等级</returns>
    private static void calculatePLW(string winSSQStr, string SSQStr, int userID, int LotteryBuyDetailID)
    {
        Model.LotteryBuyDetail model = BLL.LotteryBuyDetail.GetModel(LotteryBuyDetailID);
        //中奖号
        string[] winSSQList = winSSQStr.Split(',');
        //用户号码
        string[] SSQList = SSQStr.Split(',');
        string level = "";
        for (int n = 0; n < SSQList.Length; n++)
        {
            string[] plwList = SSQList[n].Split('*');
            int Count = 0;
            //比对红球
            for (int i = 0; i < winSSQList.Length; i++)
            {
                int index = plwList[i].IndexOf(winSSQList[i]);
                if (index > -1)
                {
                    Count++;
                }
            }
            if (Count == 5)
            {
                level += ",1";
            }
            else {
                level += ",-1";
            }
        }
        decimal winMoney = 0;
        bool isWin = false;
        int maxLevel = -1;
        string[] levelList = level.TrimStart(',').Split(',');
        for (int n = 0; n < levelList.Length; n++)
        {
            if (maxLevel < int.Parse(levelList[n]))
            {
                maxLevel = int.Parse(levelList[n]);
            }
            if (int.Parse(levelList[n]) == 1)
            {
                isWin = true;
                winMoney += 100000 * model.LotteryMultiple;
            }
            else {
                winMoney += 0;
            }
        }
        if (winMoney > 0) {
            isWin = true;
        }
        //更新数据库 
        model.IsWin = isWin;
        model.WinLevel = maxLevel;
        model.WinMoney = winMoney;
        model.Flag = true;
        BLL.LotteryBuyDetail.Update(model);
        Model.UsersAccount accout = BLL.UsersAccount.GetModelByuser_id(userID);
        accout.total = accout.total + winMoney;
        accout.winTotal = accout.winTotal + winMoney;
        BLL.UsersAccount.Update(accout);
    }

    /// <summary>
    /// 七乐彩中奖
    /// </summary>
    /// <param name="winSSQStr">开奖号码</param>
    /// <param name="SSQStr">用户购买的号码</param>
    /// <returns>等级</returns>
    private static int calculateQLC(string winSSQStr, string SSQStr)
    {
        //中奖号
        string[] winSSQList = winSSQStr.Split('+');
        string[] winBall = winSSQList[0].Split(',');
        string[] specialBall=null;

        if (winSSQList.Length > 1)
        {
             specialBall= winSSQList[1].Split(',');
        }
        //用户号码 
        SSQStr = SSQStr.Replace("$","*");
        string[] userBall = SSQStr.Split('*');  
        int level = -1;
        int ballCount = 0;
        int specialBallCount = 0;
        //比对中奖号码
        for (int i = 0; i < winBall.Length; i++)
        {
            for (int j = 0; j < userBall.Length; j++)
            {
                if (winBall[i].Equals(userBall[j]))
                {
                    ballCount++;
                    break;
                }
            }
        }
        //比对特殊号码
        if (specialBall != null)
        {
            for (int i = 0; i < specialBall.Length; i++)
            {
                for (int j = 0; j < userBall.Length; j++)
                {
                    if (specialBall[i].Equals(userBall[j]))
                    {
                        specialBallCount++;
                        break;
                    }
                }
            }
        }
        if (ballCount == 7)
        {
            level = 1;
        }
        else if (ballCount == 6 && specialBallCount == 1)
        {
            level = 2;
        }
        else if (ballCount == 6)
        {
            level = 3;
        }
        else if (ballCount == 5 && specialBallCount == 1)
        {
            level = 4;
        }
        else if (ballCount == 5)
        {
            level = 5;
        }
        else if (ballCount == 4 && specialBallCount == 1)
        {
            level = 6;
        }
        else if (ballCount == 4)
        {
            level = 7;
        }
        else
        {
            level = -1;
        }
        return level;
    }
    /// <summary>
    /// 七乐彩更新中奖信息
    /// </summary>
    /// <param name="level">中奖等级</param>
    /// <param name="userID">用户id</param>
    /// <param name="LotteryBuyDetailID">购买彩票的id</param>
    private static void updateUserAccoutByQLC(string level, int userID, int LotteryBuyDetailID)
    {
        Model.LotteryBuyDetail model = BLL.LotteryBuyDetail.GetModel(LotteryBuyDetailID);
        Model.UsersAccount accout = BLL.UsersAccount.GetModelByuser_id(userID);
        decimal winMoney = 0;
        bool isWin = true; 
        int maxLevel = -1;
        string[] levelList = level.Split(',');
        for (int n = 0; n < levelList.Length; n++)
        {
            if (maxLevel < int.Parse(levelList[n]))
            {
                maxLevel = int.Parse(levelList[n]);
            }
            if (int.Parse(levelList[n]) == 1)
            {
                winMoney += 0;
            }
            else if (int.Parse(levelList[n]) == 2)
            {
                winMoney += 0;
            }
            else if (int.Parse(levelList[n]) == 3)
            {
                winMoney += 0;
            }
            else if (int.Parse(levelList[n]) == 4)
            {
                winMoney += 200 * model.LotteryMultiple;
            }
            else if (int.Parse(levelList[n]) == 5)
            {
                winMoney += 50 * model.LotteryMultiple;
            }
            else if (int.Parse(levelList[n]) == 6)
            {
                winMoney += 10 * model.LotteryMultiple;
            }
            else if (int.Parse(levelList[n]) == 7)
            {
                winMoney += 5 * model.LotteryMultiple;
            }
            else
            {
                isWin = false;
                winMoney += 0;
            }
        }
        if (winMoney > 0) {
            isWin = true;
        }
        model.IsWin = isWin;
        model.WinLevel = maxLevel;
        model.WinMoney = winMoney;
        model.Flag = true;
        BLL.LotteryBuyDetail.Update(model);
        accout.total = accout.total + winMoney;
        accout.winTotal = accout.winTotal + winMoney;
        BLL.UsersAccount.Update(accout);
    }
    /// <summary>
    /// 七星彩中奖
    /// </summary>
    /// <param name="winSSQStr">开奖号码</param>
    /// <param name="SSQStr">用户购买的号码</param>
    /// <returns>等级</returns>
    private static int calculateQXC(string winSSQStr, string SSQStr)
    {
        //中奖号
        string[] winSSQList = winSSQStr.Split(',');
        //用户号码
        string[] SSQList = SSQStr.Split('*');

        int level = -1;
        bool one = false;
        bool two = false;
        bool three = false;
        bool four = false;
        bool five = false;
        bool six = false;
        bool seven = false;
        //比对中奖位置是否中奖号码
        for (int i = 0; i < winSSQList.Length; i++)
        {
            int index = SSQList[i].IndexOf(winSSQList[i]);
            if (index > -1)
            {
                if (i == 0) { one = true; }
                else if (i == 1) { two = true; }
                else if (i == 2) { three = true; }
                else if (i == 3) { four = true; }
                else if (i == 4) { five = true; }
                else if (i == 5) { six = true; }
                else if (i == 6) { seven = true; }
            }
        }
        if (one && two && three && four && five && six && seven)
        {
            level = 1;
        }
        else if ((one && two && three && four && five && six) || (two && three && four && five && six && seven))
        {
            level = 2;
        }
        else if ((one && two && three && four && five) || (two && three && four && five && six) || (three && four && five && six && seven))
        {
            level = 3;
        }
        else if ((one && two && three && four) || (two && three && four && five) || (three && four && five && six) || (four && five && six && seven))
        {
            level = 4;
        }
        else if ((one && two && three) || (two && three && four) || (three && four && five) || (four && five && six) || (five && six && seven))
        {
            level = 5;
        }
        else if ((one && two) || (two && three) || (three && four) || (four && five) || (five && six) || (seven && six))
        {
            level = 6;
        }
        return level;
    }
    /// <summary>
    /// 七星彩更新中奖信息
    /// </summary>
    /// <param name="level">中奖等级</param>
    /// <param name="userID">用户id</param>
    /// <param name="LotteryBuyDetailID">购买彩票的id</param>
    private static void updateUserAccoutByQXC(string level, int userID, int LotteryBuyDetailID)
    {
        Model.LotteryBuyDetail model = BLL.LotteryBuyDetail.GetModel(LotteryBuyDetailID);
        Model.UsersAccount accout = BLL.UsersAccount.GetModelByuser_id(userID);
        decimal winMoney = 0;
        bool isWin = true;
        int maxLevel = -1;
        string[] levelList = level.Split(',');
        for (int n = 0; n < levelList.Length; n++)
        {
            if (maxLevel < int.Parse(levelList[n]))
            {
                maxLevel = int.Parse(levelList[n]);
            }
            if (int.Parse(levelList[n]) == 1)
            {
                winMoney += 0;
            }
            else if (int.Parse(levelList[n]) == 2)
            {
                winMoney += 0;
            }
            else if (int.Parse(levelList[n]) == 3)
            {
                winMoney+= 1800 * model.LotteryMultiple;
            }
            else if (int.Parse(levelList[n]) == 4)
            {
                winMoney += 300 * model.LotteryMultiple;
            }
            else if (int.Parse(levelList[n]) == 5)
            {
                winMoney += 20 * model.LotteryMultiple;
            }
            else if (int.Parse(levelList[n]) == 6)
            {
                winMoney += 5 * model.LotteryMultiple;
            }
            else
            {
                isWin = false;
                winMoney += 0;
            }
        }
        if (winMoney > 0) {
            isWin = true;
        }
        model.IsWin = isWin;
        model.WinLevel = maxLevel;
        model.WinMoney = winMoney;
        model.Flag = true;
        BLL.LotteryBuyDetail.Update(model);
        accout.total = accout.total + winMoney;
        accout.winTotal = accout.winTotal + winMoney;
        BLL.UsersAccount.Update(accout);
    }
    /// <summary>
    /// 排列三中奖
    /// </summary>
    /// <param name="winSSQStr">开奖号码</param>
    /// <param name="SSQStr">用户购买的号码</param>
    /// <returns>等级</returns>
    private static int calculatePLS(string winSSQStr, string SSQStr)
    {
        List<string> lastList = new List<string>();
        //中奖号
        string[] winSSQList = winSSQStr.Split(',');
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                for (int k = 0; k < 3; k++)
                {
                    if (i != j && i != k && j != k)
                    {
                        string listStr = winSSQList[i] + "," + winSSQList[j] + "," + winSSQList[k];
                        lastList.Add(listStr);
                    }
                }
            }
        }
        //用户号码
        string[] SSQList = SSQStr.Split('*');

        int level = -1;
        int Count = 0;
        //直选 	与开奖号相同，顺序一致 一等奖
        for (int i = 0; i < winSSQList.Length; i++)
        {
            int index = SSQList[i].IndexOf(winSSQList[i]);
            if (index > -1)
            {
                Count++;
            }
        }

        if (Count == 3)
        {
            level = 1;
            return level;
        }
        //组三
        if ((winSSQList[0] == winSSQList[1] && winSSQList[0] != winSSQList[2])
            || (winSSQList[0] != winSSQList[1] && winSSQList[1] == winSSQList[2])
            || (winSSQList[0] == winSSQList[2] && winSSQList[0] != winSSQList[1]))
        {
            for (int i = 0; i < lastList.Count; i++)
            {
                int countFlag = 0;
                string[] flagList = lastList[i].Split(',');
                for (int j = 0; j < flagList.Length; j++)
                {
                    int index = SSQList[j].IndexOf(flagList[j]);
                    if (index > -1)
                    {
                        countFlag++;
                    }
                    if (countFlag == 3)
                    {
                        level = 2;
                        return level;
                    }
                }
            }
        }
        //组六
        if (winSSQList[0] != winSSQList[1] && winSSQList[0] != winSSQList[2]
            && winSSQList[1] != winSSQList[2] && (int.Parse(winSSQList[0]) + 2) != int.Parse(winSSQList[2]))
        {
            for (int i = 0; i < lastList.Count; i++)
            {
                int countFlag = 0;
                string[] flagList = lastList[i].Split(',');
                for (int j = 0; j < flagList.Length; j++)
                {
                    int index = SSQList[j].IndexOf(flagList[j]);
                    if (index > -1)
                    {
                        countFlag++;
                    }
                    if (countFlag == 3)
                    {
                        level = 3;
                        return level;
                    }
                }
            }
        }
        return level;
    }
    /// <summary>
    /// 排列三更新中奖信息
    /// </summary>
    /// <param name="level">中奖等级</param>
    /// <param name="userID">用户id</param>
    /// <param name="LotteryBuyDetailID">购买彩票的id</param>
    private static void updateUserAccoutByPLS(string level, int userID, int LotteryBuyDetailID)
    {
        Model.LotteryBuyDetail model = BLL.LotteryBuyDetail.GetModel(LotteryBuyDetailID);
        Model.UsersAccount accout = BLL.UsersAccount.GetModelByuser_id(userID);
        decimal winMoney = 0;
        bool isWin = true;
        int maxLevel = -1;
        string[] levelList = level.Split(',');
        for (int n = 0; n < levelList.Length; n++)
        {
            if (maxLevel < int.Parse(levelList[n]))
            {
                maxLevel = int.Parse(levelList[n]);
            }
            if (int.Parse(levelList[n]) == 1)
            {
                winMoney += 1040 * model.LotteryMultiple;
            }
            else if (int.Parse(levelList[n]) == 2)
            {
                winMoney += 346 * model.LotteryMultiple;
            }
            else if (int.Parse(levelList[n]) == 3)
            {
                winMoney += 173 * model.LotteryMultiple;
            }
            else
            {
                isWin = false;
                winMoney += 0;
            }
        }
        if (winMoney > 0)
        {
            isWin = true;
        }
        model.IsWin = isWin;
        model.WinLevel = maxLevel;
        model.WinMoney = winMoney;
        model.Flag = true;
        BLL.LotteryBuyDetail.Update(model);
        accout.total = accout.total + winMoney;
        accout.winTotal = accout.winTotal + winMoney;
        BLL.UsersAccount.Update(accout);
    }
    /// <summary>
    /// 福彩
    /// </summary>
    /// <param name="winSSQStr">开奖号</param>
    /// <param name="SSQStr">用户号码</param>
    /// <param name="SSQStr">用户购买的类型1:直选, 2:通选,3:组三,4:组六,5:和数,6:包选，7:1D，8:2D,9:大小奇偶,10:拖拉机，11:猜三同 </param>
    public static void calculateFC(string winSSQStr, string SSQStr, int userID, int LotteryBuyDetailID, int fcType, int fcTypeNext)
    {   //中奖号
        string[] winSSQList = winSSQStr.Split(',');
        //用户号码
        string[] SSQList = SSQStr.Split(',');
        //直选
        if (fcType == 1)
        {
            ZX(winSSQList, SSQList, userID, LotteryBuyDetailID);
        }
        //通选
        else if (fcType == 2)
        {
            TX(winSSQList, SSQList, userID, LotteryBuyDetailID);
        }
        //组三
        else if (fcType == 3)
        {
            ZS(winSSQStr, SSQList, userID, LotteryBuyDetailID);
        }
        //组六
        else if (fcType == 4)
        {
            ZL(winSSQStr, SSQList, userID, LotteryBuyDetailID);
        }
        //和数
        else if (fcType == 5)
        {
            HS(winSSQList, SSQStr, userID, LotteryBuyDetailID);
        }
        //包选
        else if (fcType == 6)
        {
            BX(winSSQList, SSQList, userID, LotteryBuyDetailID);
        }
        //1D
        else if (fcType == 7)
        {
            Do1D(winSSQList, SSQList, userID, LotteryBuyDetailID);
        }
        //2D
        else if (fcType == 8)
        {
            Do2D(winSSQList, SSQList, userID, LotteryBuyDetailID);
        }
        //大小奇偶
        else if (fcType == 9)
        {
            CST(winSSQList, userID, LotteryBuyDetailID, fcType, fcTypeNext);
        }
        //拖拉机
        else if (fcType == 10)
        {
            CST(winSSQList, userID, LotteryBuyDetailID, fcType, fcTypeNext);
        }
        //猜三同
        else if (fcType == 11)
        {
            CST(winSSQList, userID, LotteryBuyDetailID, fcType, fcTypeNext);
        }
    }
    /// <summary>
    /// 直选
    /// </summary>
    public static void ZX(string[] winSSQList, string[] SSQList, int userID, int LotteryBuyDetailID)
    {
        Model.LotteryBuyDetail model = BLL.LotteryBuyDetail.GetModel(LotteryBuyDetailID);
        Model.UsersAccount accout = BLL.UsersAccount.GetModelByuser_id(userID);
        bool isWin = false;
        decimal winMoney = 0;
        int maxLevel = -1;
        int[] level = new int[] { 0, 0, 0, 0, 0, 0 };
        for (int n = 0; n < SSQList.Length; n++)
        {
            int count = 0;
            string[] fcList = SSQList[n].Split('*');
            for (int i = 0; i < winSSQList.Length; i++)
            {
                int index = fcList[i].IndexOf(winSSQList[i]);
                if (index > -1)
                {
                    count++;
                }
            }
            if (count == 3)
            { 
                winMoney += 1040 * model.LotteryMultiple;
                level[0] += 1;
            }
        }
        if (winMoney > 0)
        {
            isWin = true;
            level[1] = level[2] = level[3] = level[4] = level[5] = 0;
            maxLevel = 1;
        }
        model.IsWin = isWin;
        model.WinLevel = maxLevel;
        model.WinMoney = winMoney;
        model.Flag = true;
        BLL.LotteryBuyDetail.Update(model);
        accout.total = accout.total + winMoney;
        accout.winTotal = accout.winTotal + winMoney;
        BLL.UsersAccount.Update(accout);
        sumWinMoney = winMoney;
        addWinInfo(level, model.LotteryType, model.LotteryQs, LotteryBuyDetailID);
    }
    public static void TX(string[] winSSQList, string[] SSQList, int userID, int LotteryBuyDetailID)
    {
        int maxLevel = -1;
        int[] level = new int[] { 0, 0, 0, 0, 0, 0 };
        Model.LotteryBuyDetail model = BLL.LotteryBuyDetail.GetModel(LotteryBuyDetailID);
        Model.UsersAccount accout = BLL.UsersAccount.GetModelByuser_id(userID);
        bool isWin = false;
        decimal winMoney = 0; 
        for (int n = 0; n < SSQList.Length; n++)
        {
            int count = 0;
            string[] fcList = SSQList[n].Split('*');
            for (int i = 0; i < winSSQList.Length; i++)
            {
                int index = fcList[i].IndexOf(winSSQList[i]);
                if (index > -1)
                {
                    count++;
                }
            }
            if (count == 3)
            {
                isWin = true;
                winMoney += 470 * model.LotteryMultiple;
            }
            else if (count == 2)
            {
                isWin = true;
                winMoney += 21 * model.LotteryMultiple;
            }
        }
        if (winMoney > 0)
        {
            isWin = true;
            maxLevel = 2;
        }
        model.IsWin = isWin;
        model.WinLevel = maxLevel;
        model.WinMoney = winMoney;
        model.Flag = true;
        BLL.LotteryBuyDetail.Update(model);
        accout.total = accout.total + winMoney;
        accout.winTotal = accout.winTotal + winMoney;
        BLL.UsersAccount.Update(accout);
        sumWinMoney = winMoney;
        addWinInfo(level, model.LotteryType, model.LotteryQs, LotteryBuyDetailID);
    }
    public static void ZS(string winSSQStr, string[] SSQList, int userID, int LotteryBuyDetailID)
    {
        int count = 0;
        int maxLevel = -1;
        int[] level = new int[] { 0, 0, 0, 0, 0, 0 };
        Model.LotteryBuyDetail model = BLL.LotteryBuyDetail.GetModel(LotteryBuyDetailID);
        Model.UsersAccount accout = BLL.UsersAccount.GetModelByuser_id(userID);
        bool isWin = false;
        decimal winMoney = 0;
        List<string> lastList = getWinNoSort(winSSQStr);
        string[] winSSQList = winSSQStr.Split(',');
        if ((winSSQList[0] == winSSQList[1] && winSSQList[0] != winSSQList[2])
            || (winSSQList[0] != winSSQList[1] && winSSQList[1] == winSSQList[2])
            || (winSSQList[0] == winSSQList[2] && winSSQList[0] != winSSQList[1]))
        {
            for (int n = 0; n < SSQList.Length; n++)
            {
                string[] fcList = SSQList[n].Split('*');
                for (int i = 0; i < lastList.Count; i++)
                {
                    int countFlag = 0;
                    string[] flagList = lastList[i].Split(',');
                    for (int j = 0; j < flagList.Length; j++)
                    {
                        int index = fcList[j].IndexOf(flagList[j]);
                        if (index > -1)
                        {
                            countFlag++;
                        }
                        if (countFlag == 3)
                        {
                            isWin = true;
                            winMoney += 346 * model.LotteryMultiple;
                            count++;
                        }
                    }
                }
            }
        }
        if (winMoney > 0)
        {
            isWin = true;
            level[1] = count / 2;
            level[0] = level[2] = level[3] = level[4] = level[5] = 0;
            maxLevel = 3;
        }
        model.IsWin = isWin;
        model.WinLevel = maxLevel;
        model.WinMoney = winMoney/2;
        model.Flag = true;
        BLL.LotteryBuyDetail.Update(model);
        accout.total = accout.total + winMoney / 2;
        accout.winTotal = accout.winTotal + winMoney / 2;
        BLL.UsersAccount.Update(accout);
        sumWinMoney = winMoney / 2;
        addWinInfo(level, model.LotteryType, model.LotteryQs, LotteryBuyDetailID);
    }
    public static void ZL(string winSSQStr, string[] SSQList, int userID, int LotteryBuyDetailID)
    {
        int maxLevel = -1;
        int[] level = new int[] { 0, 0, 0, 0, 0, 0 };
        Model.LotteryBuyDetail model = BLL.LotteryBuyDetail.GetModel(LotteryBuyDetailID);
        Model.UsersAccount accout = BLL.UsersAccount.GetModelByuser_id(userID);
        bool isWin = false;
        decimal winMoney = 0;
        List<string> lastList = getWinNoSort(winSSQStr);
        string[] winSSQList = winSSQStr.Split(',');
        Array.Sort(winSSQList);
        if (winSSQList[0] != winSSQList[1] && winSSQList[0] != winSSQList[2]
            && winSSQList[1] != winSSQList[2] && (int.Parse(winSSQList[0]) + 2) != int.Parse(winSSQList[2]))
        {
            for (int n = 0; n < SSQList.Length; n++)
            {
                string[] fcList = SSQList[n].Split('*');

                for (int i = 0; i < lastList.Count; i++)
                {
                    int countFlag = 0;
                    string[] flagList = lastList[i].Split(',');
                    for (int j = 0; j < flagList.Length; j++)
                    {
                        int index = fcList[j].IndexOf(flagList[j]);
                        if (index > -1)
                        {
                            countFlag++;
                        }
                        if (countFlag == 3)
                        {
                            winMoney += 173 * model.LotteryMultiple;
                            level[2] += 1;
                        }
                    }
                }
            }
        }
        if (winMoney > 0)
        {
            level[0] = level[1] = level[3] = level[4] = level[5] = 0;
            isWin = true;
            maxLevel = 4;
        }
        model.IsWin = isWin;
        model.WinLevel = maxLevel;
        model.WinMoney = winMoney;
        model.Flag = true;
        BLL.LotteryBuyDetail.Update(model);
        accout.total = accout.total + winMoney;
        accout.winTotal = accout.winTotal + winMoney;
        BLL.UsersAccount.Update(accout);
        sumWinMoney = winMoney;
        addWinInfo(level, model.LotteryType, model.LotteryQs, LotteryBuyDetailID);
    }
    public static void BX(string[] winSSQList, string[] SSQList, int userID, int LotteryBuyDetailID)
    {
        int maxLevel = -1;
        int[] level = new int[] { 0, 0, 0, 0, 0, 0 };
        Model.LotteryBuyDetail model = BLL.LotteryBuyDetail.GetModel(LotteryBuyDetailID);
        Model.UsersAccount accout = BLL.UsersAccount.GetModelByuser_id(userID);
        bool isWin = false;
        decimal winMoney = 0;
        for (int n = 0; n < SSQList.Length; n++)
        {
            int count = 0;
            string[] fcList = SSQList[n].Split('*');

            for (int j = 0; j < winSSQList.Length; j++)
            {
                int index = fcList[j].IndexOf(winSSQList[j]);
                if (index > -1)
                {
                    count++;
                }
                if (count == 2)
                {
                    isWin = true;
                    winMoney += 693 * model.LotteryMultiple;
                }
            }
        }
        string winstr = winSSQList[0] + "," + winSSQList[1] + "," + winSSQList[2];
        List<string> lastList = getWinNoSort(winstr);
        for (int n = 0; n < SSQList.Length; n++)
        {
            string[] fcList = SSQList[n].Split('*');

            for (int i = 0; i < lastList.Count; i++)
            {
                int countFlag = 0;
                string[] flagList = lastList[i].Split(',');
                for (int j = 0; j < flagList.Length; j++)
                {
                    int index = fcList[j].IndexOf(flagList[j]);
                    if (index > -1)
                    {
                        countFlag++;
                    }
                    if (countFlag == 3)
                    {
                        winMoney += 173 * model.LotteryMultiple;
                    }
                }
            }
        }
        if (winMoney > 0)
        {
            isWin = true;
            maxLevel = 6;
        }
        model.IsWin = isWin;
        model.WinLevel = maxLevel;
        model.WinMoney = winMoney;
        model.Flag = true;
        BLL.LotteryBuyDetail.Update(model);
        accout.total = accout.total + winMoney;
        accout.winTotal = accout.winTotal + winMoney;
        BLL.UsersAccount.Update(accout);
        sumWinMoney = winMoney;
        addWinInfo(level, model.LotteryType, model.LotteryQs, LotteryBuyDetailID);
    }
    public static void Do1D(string[] winSSQList, string[] SSQList, int userID, int LotteryBuyDetailID)
    {
        int maxLevel = -1;
        int[] level = new int[] { 0, 0, 0, 0, 0, 0 };
        Model.LotteryBuyDetail model = BLL.LotteryBuyDetail.GetModel(LotteryBuyDetailID);
        Model.UsersAccount accout = BLL.UsersAccount.GetModelByuser_id(userID);
        bool isWin = false;
        decimal winMoney = 0;
        for (int n = 0; n < SSQList.Length; n++)
        {
            string[] fcList = SSQList[n].Split('*');
            for (int j = 0; j < winSSQList.Length; j++)
            {
                int count = 0;
                int index = fcList[j].IndexOf(winSSQList[j]);
                if (index > -1)
                {
                    count++;
                }
                if (count == 1)
                {
                    winMoney += 10 * model.LotteryMultiple;
                }
            }
        }
        if (winMoney > 0)
        {
            isWin = true;
            maxLevel = 5;
        }
        model.IsWin = isWin;
        model.WinLevel = maxLevel;
        model.WinMoney = winMoney;
        model.Flag = true;
        BLL.LotteryBuyDetail.Update(model);
        accout.total = accout.total + winMoney;
        accout.winTotal = accout.winTotal + winMoney;
        BLL.UsersAccount.Update(accout);
        sumWinMoney = winMoney;
        addWinInfo(level, model.LotteryType, model.LotteryQs, LotteryBuyDetailID);
    }
    public static void Do2D(string[] winSSQList, string[] SSQList, int userID, int LotteryBuyDetailID)
    {
        int maxLevel = -1;
        int[] level = new int[] { 0, 0, 0, 0, 0, 0 };
        Model.LotteryBuyDetail model = BLL.LotteryBuyDetail.GetModel(LotteryBuyDetailID);
        Model.UsersAccount accout = BLL.UsersAccount.GetModelByuser_id(userID);
        bool isWin = false;
        decimal winMoney = 0;
        for (int n = 0; n < SSQList.Length; n++)
        {
            string[] fcList = SSQList[n].Split('*');
            int count = 0;
            for (int j = 0; j < winSSQList.Length; j++)
            {
                int index = fcList[j].IndexOf(winSSQList[j]);
                if (index > -1)
                {
                    count++;
                }
                if (count == 2)
                {
                    isWin = true;
                    winMoney += 104 * model.LotteryMultiple;
                }
            }
        } 
        if (winMoney > 0)
        {
            isWin = true;
            maxLevel = 6;
        }
        model.IsWin = isWin;
        model.WinLevel = maxLevel;
        model.WinMoney = winMoney;
        model.Flag = true;
        BLL.LotteryBuyDetail.Update(model);
        accout.total = accout.total + winMoney;
        accout.winTotal = accout.winTotal + winMoney;
        BLL.UsersAccount.Update(accout);
        sumWinMoney = winMoney;
        addWinInfo(level, model.LotteryType, model.LotteryQs, LotteryBuyDetailID);
    }
    public static void HS(string[] winSSQList, string total, int userID, int LotteryBuyDetailID)
    {
        int[] level = new int[] { 0, 0, 0, 0, 0, 0 };
        Model.LotteryBuyDetail model = BLL.LotteryBuyDetail.GetModel(LotteryBuyDetailID);
        Model.UsersAccount accout = BLL.UsersAccount.GetModelByuser_id(userID);
        bool isWin = false;
        decimal winMoney = 0;
        decimal sum = 0;
        for (int i = 0; i < winSSQList.Length; i++)
        {
            sum += decimal.Parse(winSSQList[i]);
        }
        string[] hslist = total.Split(',');
        for (int i = 0; i < hslist.Length; i++)
        {
            if (sum == decimal.Parse(hslist[i]))
            {
                isWin = true;
                if (sum == 0 || sum == 27)
                {
                    winMoney += 1040 * model.LotteryMultiple;
                }
                else if (sum == 1 || sum == 26)
                {
                    winMoney += 345 * model.LotteryMultiple;
                }
                else if (sum == 2 || sum == 25)
                {
                    winMoney += 172 * model.LotteryMultiple;
                }
                else if (sum == 3 || sum == 24)
                {
                    winMoney += 104 * model.LotteryMultiple;
                }
                else if (sum == 4 || sum == 23)
                {
                    winMoney += 69 * model.LotteryMultiple;
                }
                else if (sum == 5 || sum == 22)
                {
                    winMoney += 49 * model.LotteryMultiple;
                }
                else if (sum == 6 || sum == 21)
                {
                    winMoney += 37 * model.LotteryMultiple;
                }
                else if (sum == 7 || sum == 20)
                {
                    winMoney += 29 * model.LotteryMultiple;
                }
                else if (sum == 8 || sum == 19)
                {
                    winMoney += 23 * model.LotteryMultiple;
                }
                else if (sum == 9 || sum == 18)
                {
                    winMoney += 19 * model.LotteryMultiple;
                }
                else if (sum == 10 || sum == 17)
                {
                    winMoney += 16 * model.LotteryMultiple;
                }
                else if (sum == 11 || sum == 16)
                {
                    winMoney += 15 * model.LotteryMultiple;
                }
                else if (sum == 12 || sum == 15)
                {
                    winMoney += 15 * model.LotteryMultiple;
                }
                else if (sum == 13 || sum == 14)
                {
                    winMoney += 14 * model.LotteryMultiple;
                }
            }
        }
        model.IsWin = isWin;
        model.WinLevel = 0;
        model.WinMoney = winMoney;
        model.Flag = true;
        BLL.LotteryBuyDetail.Update(model);
        accout.total = accout.total + winMoney;
        accout.winTotal = accout.winTotal + winMoney;
        BLL.UsersAccount.Update(accout);
        sumWinMoney = winMoney;
        addWinInfo(level, model.LotteryType, model.LotteryQs, LotteryBuyDetailID);
    }
    public static void CST(string[] winSSQList, int userID, int LotteryBuyDetailID, int fcType, int fcTypeNext)
    {
        int maxLevel = -1;
        int[] level = new int[] { 0, 0, 0, 0, 0, 0 };
        Model.LotteryBuyDetail model = BLL.LotteryBuyDetail.GetModel(LotteryBuyDetailID);
        Model.UsersAccount accout = BLL.UsersAccount.GetModelByuser_id(userID);
        bool isWin = false;
        decimal winMoney = 0;
        int hs = int.Parse(winSSQList[0]) + int.Parse(winSSQList[1]) + int.Parse(winSSQList[2]);
        string tlj=winSSQList[0]+winSSQList[1]+winSSQList[2];
        if (fcType == 11)
        {
            if (int.Parse(winSSQList[0]) == int.Parse(winSSQList[1]) && int.Parse(winSSQList[1]) == int.Parse(winSSQList[2]))
            {
                isWin = true;
                winMoney += 104 * model.LotteryMultiple * model.LotteryCount;
                maxLevel = 11;
            }
        }
        else if (fcType == 10)
        { 
            if (tlj == "012" || tlj == "123" || tlj == "234" || tlj == "345" || tlj == "456" || tlj == "567" || tlj == "678" || tlj == "789" 
                || tlj == "210" || tlj == "321" || tlj == "432" || tlj == "543" || tlj == "654" || tlj == "765" || tlj == "876" || tlj == "987")
            {
                isWin = true;
                winMoney += 65 * model.LotteryMultiple * model.LotteryCount;
                maxLevel = 10;
            }
        }
        else if (fcType == 9)
        { 
            string[] lotteryList=model.LotteryNo.Split(',');
            //int a = int.Parse(tlj) % 2;
            foreach (string str in lotteryList)
            {
                if ((str.Equals("1") && hs >= 19 && hs <= 27) || (str.Equals("2") && hs >= 0 && hs <= 8))
                {
                    isWin = true;
                    winMoney += 6 * model.LotteryMultiple;
                    maxLevel = 9;
                }
                else if ((str.Equals("3") && int.Parse(tlj) % 2 == 1) || (str.Equals("4") && int.Parse(tlj) %2 == 0))
                {
                    isWin = true;
                    winMoney += 8 * model.LotteryMultiple;
                    maxLevel = 9;
                }
            }
        }
        model.IsWin = isWin;
        model.WinLevel = maxLevel;
        model.WinMoney = winMoney;
        model.Flag = true;
        BLL.LotteryBuyDetail.Update(model);
        accout.total = accout.total + winMoney;
        accout.winTotal = accout.winTotal + winMoney;
        BLL.UsersAccount.Update(accout);
        sumWinMoney = winMoney;
        addWinInfo(level, model.LotteryType, model.LotteryQs, LotteryBuyDetailID);
    }

    /// <summary>
    /// 将中奖号码排列成不同的组合
    /// </summary>
    /// <param name="winSSQStr"></param>
    /// <returns></returns>
    public static  List<string> getWinNoSort(string winSSQStr)
    {
        List<string> lastList = new List<string>();
        //中奖号
        string[] winSSQList = winSSQStr.Split(',');
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                for (int k = 0; k < 3; k++)
                {
                    if (i != j && i != k && j != k)
                    {
                        string listStr = winSSQList[i] + "," + winSSQList[j] + "," + winSSQList[k];
                        lastList.Add(listStr);
                    }
                }
            }
        }
        return lastList;
    }
    private static int Ccombo(int e, int n)
    {
        if (n <= e)
        {
            var r = Tank(e - n) * Tank(n);
            return r = Tank(e) / r;
        }
        return 0;
    }
    private static int Tank(int e)
    {
        return e == 0 ? 1 : Tank(e - 1) * e;
    }
    private static int Rank(int e, int t)
    {
        var n = e * 1 + t * 1;
        switch (n)
        {
            case 7:
                return 0;
            case 6:
                return t == 1 ? 2 : 1;
            case 5:
                return 3;
            case 4:
                return 4;
            default:
                return t == 1 ? 5 : -1;
        }
    }
    private static int combo(int e, int t)
    {
        if (e / 2 < t)
        {
            t = e - t;
        }
        if (e < t || t < 0) return 0;
        if (e >= 0 && t == 0) return 1;
        int n = 1;
        int r = e;
        for (var i = 1; i <= t; i++)
        {
            n *= i;
            if (i < t)
            {
                r *= e - i;
            }
        }
        return r / n;
    }
    private static int Rankx(int e, int t)
    {
        var n = e * 1 + t * 1;
        switch (n)
        {
            case 7:
                return 0;
            case 6:
                return t == 1 ? 1 : 2;
            case 5:
                return t == 0 ? 2 : 3;
            case 4:
                return 4;
            case 3:
                return 5;
            case 2:
                return e == 0 ? 5 : -1;
            default:
                return -1;
        }
    }
    /// <summary>
    /// 插入中奖信息
    /// </summary>
    /// <param ></param>
    private static void addWinInfo(int[] level, int lotteryType, string lotteryQs,int LotteryBuyDetailID)
    {
        //3d直选个数oneCount,组三个数twoCount，组六个数threeCount，其他sixCount
        if (lotteryType == 5)
        {
            string sql = string.Format("SELECT top 1 * FROM LotteryAnnouncement WHERE LotteryType={0} and LotteryQS={1}", 5, lotteryQs);
            la = BLL.LotteryAnnouncement.GetModelBySql(sql);
        }
        Model.LotteryAnnouncementMy modelMy = new Model.LotteryAnnouncementMy();
        if (la != null)
        {
            modelMy.oneMoney = la.oneMoney;
            modelMy.twoMoney = la.twoMoney;
            modelMy.threeMoney = la.threeMoney;
            modelMy.fourMoney = la.fourMoney;
            modelMy.fiveMoney = la.fiveMoney;
            modelMy.sixMoney = la.sixMoney;
            modelMy.openTime = la.openTime;
            modelMy.lotteryNo = la.LotteryNo;
        }
        modelMy.oneCount = (int)level[0];
        modelMy.twoCount = (int)level[1];
        modelMy.threeCount = (int)level[2];
        modelMy.fourCount = (int)level[3];
        modelMy.fiveCount = (int)level[4];
        modelMy.sixCount = (int)level[5];
        modelMy.sumMoney = sumWinMoney;
        modelMy.lotteryType = lotteryType;
        modelMy.lotteryQS = lotteryQs;
        modelMy.addTime = DateTime.Now;
        modelMy.isChipped = false;
        modelMy.buyId = LotteryBuyDetailID;
        BLL.LotteryAnnouncementMy.Add(modelMy);
    }
}